//
//  SearchPresentViewController.m
//  礼物go
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchPresentViewController.h"

@interface SearchPresentViewController ()

@end

@implementation SearchPresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"";
    //[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = nil;
   // [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.rightBarButtonItem = nil;
   // [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    
    self.hotCollectionView.frame = CGRectMake(0, 40, SCREENWIDTH, self.view.frame.size.height - 49);
    
    
    
    
    // Do any additional setup after loading the view.
}

-(void)setSearchPresentString:(NSString *)searchPresentString{
    _searchPresentString = searchPresentString;

    [self getSelectList:0];
}



-(void)getSelectList:(NSUInteger)nextCursor{
    
    AFNetworkReachabilityManager *netManager = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=%ld&sort=",self.searchPresentString, 20 * nextCursor];
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        [netManager stopMonitoring];
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.dataArray removeAllObjects];
        }
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *itemArray = [dataDic objectForKey:@"items"];
        for (NSDictionary *dic in itemArray) {
            HotColumnModel *hotModel = [[HotColumnModel alloc] init];
            [hotModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:hotModel];
        }
        [self.hotCollectionView.mj_header endRefreshing];
        [self.hotCollectionView.mj_footer endRefreshing];
        [self.hotCollectionView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HotColumnCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotCell forIndexPath:indexPath];
    HotColumnModel *hotModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.hotModel = hotModel;
  
   // NSLog(@"%@",hotModel.name);
    
    if (_searchPresentString != nil) {
        //设置字体自高亮
        NSString *resultStr = hotModel.name;
        // NSLog(@"%@",hotModel.name);
        //获取输入框的内容
        NSString *searchStr = self.searchPresentString;
        //NSLog(@"%@",_searchPresentString);
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:resultStr];
        
        //范围
        NSRange range = [resultStr rangeOfString:searchStr];
        //设置字体颜色
        [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
        // NSForegroundColorAttributeName 颜色
        // NSFontAttributeName 字体
        // NSBackgroundColorAttributeName 背景颜色
        
        cell.nameLabel.attributedText = attributeStr;
 
    }
    
    
   
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
